BITS 32
%include "macrosmodoprotegido.mac"

extern fin_intr_pic1
extern kprintf
extern pagina_libre_usuario
extern mapear_pagina
extern tsss
extern obtener_virtual_libre

global _isr0
global _isr1
global _isr2
global _isr3
global _isr4
global _isr5
global _isr6
global _isr7
global _isr8
global _isr9
global _isr10
global _isr11
global _isr12
global _isr13
global _isr14
global _isr15
global _isr16
global _isr17
global _isr18
global _isr19

global _isrReloj
global _isrTeclado

global _isr88
global _isr89


_isr0:
	;mov edi, isr0_msg
	;IMPRIMIR_TEXTO edi, 32, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr0_msg
	call kprintf
	add esp, 16
	push dword 0
	jmp _informarEstado


_isr1:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr1_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr2:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr2_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr3:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr3_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr4:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr4_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado
	

_isr5:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr5_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado
	

_isr6:
	cli
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr6_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr7:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr7_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr8:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr8_msg
	call kprintf
	add esp, 16	
	jmp _informarEstado
	

_isr9:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr9_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr10:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr10_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado
	

_isr11:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr11_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado

	
_isr12:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr12_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado
	

_isr13:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr13_msg
	call kprintf
	add esp, 16	
	; -----------pushea  otra cosa mas ...
	jmp _informarEstado


_isr14:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr14_msg
	call kprintf
	add esp, 16	
	;~ jmp noMostrar
	jmp _informarEstado
	

_isr15:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr15_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado
	

_isr16:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr16_msg
	call kprintf
	add esp, 16
	push dword 0
	jmp _informarEstado

	
_isr17:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2 
	push dword blanco_sobre_bordo
	push isr17_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado
	

_isr18:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr18_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado


_isr19:
	;mov edi, isr1_msg
	;IMPRIMIR_TEXTO edi, 21, 0x0A, 0, 0
	push dword 1
	push dword 2
	push dword blanco_sobre_bordo
	push isr19_msg
	call kprintf
	add esp, 16	
	push dword 0
	jmp _informarEstado	





_isrReloj:
	cli
	pusha
	call fin_intr_pic1
	call proximo_reloj	
	
	str ax
	cmp ax, 0x38
    je tarea2
    cmp ax,0x40
    je tarea3
    cmp ax,0x48
    je tarea4
    cmp ax,0x50
    je tarea1
    jmp fin
tarea1:
    jmp 0x38:0
    jmp fin
tarea2:
	jmp 0x40:0
    jmp fin
tarea3:
    jmp 0x48:0
    jmp fin
tarea4:
    jmp 0x50:0
    jmp fin        
	
  
  		
	
  fin:
	popa
	sti
	iret

array_noNUMPAD: db "1234567890"
arrayNUMPAD: db "789-456+1230"

	
_isrTeclado:
	cli
	pusha
	call fin_intr_pic1	

	pushf	; guarda los flags
	
	xor eax, eax
	in al, 0x60
			
	test al, 0x80	; TRUE si fue soltada
	
	jne .caso_SoltarTecla

	.caso_pulsarTecla:
		; ScanCode 2 a 10
		cmp al, 2
		jb .fin
		cmp al, 11
		ja .NumPad
			
			mov ebx, array_noNUMPAD
			add ebx, eax
			dec ebx
			dec ebx
			mov byte al, [ebx]		; el caracter es array_noNUMPAD[scancode - 2]
			jmp .imprimir
			
	
		.NumPad:
		cmp al, 71
		jb .fin
		cmp al, 82
		ja .fin	
		
			cmp al, 74		;descarta -
			je .fin
			cmp al, 78		;descarta +
			je .fin
	
			mov ebx, arrayNUMPAD
			add ebx, eax
			sub ebx, 71
			mov byte al, [ebx]		; el caracter es array_noNUMPAD[scancode - 71]
	
	.imprimir:

	mov [0xB8000+24*80*2], al		;imprime numero
	mov al, 0xF
	mov [0xB8000+24*80*2+1], al		; (color blanco)
	jmp .fin
	

	.caso_SoltarTecla:
	xor al, al
	mov [0xB8000+24*80*2+1], al		; borra numero

	.fin:
	popf
	popa
	sti
	iret	
	


%define Columna dword[ebp+8]
%define B dword[ebp+12]
%define C dword[ebp+16]
%define D dword[ebp+20]


_isr88:
	cli

	push D
	push C
	push B
	push A
	call kprintf
	add esp,16

	sti
	iret


_isr89:
	cli
	push ebx
	push edi
	push esi
	
	 mov eax, ds
	 push eax
	 mov eax, es
	 push eax
	 mov ax, 0x10
	 mov ds, ax
	 mov ax, 0x10
	 mov es, ax
	;~

	mov eax, cr3
	mov edi, eax		; edi = cr3 usuario
	
	mov eax, 0x100000	;   cambia al directorio
	mov cr3, eax		;    ----> del kernel para poder mapear
	

	; OBTENER PAGINA VIRTUAL LIBRE
	push edi
	call obtener_virtual_libre
	add esp, 4
	mov ebx, eax		;ebx= virtual
	
	; OBTENER PAGINA FISICA LIBRE
	call pagina_libre_usuario 
	;eax = fisica
	
	
	; MAPEAR
	push 7			;atributo
	push eax		;fisica
	push edi		;cr3
	push ebx		;virtual
	call mapear_pagina
	add esp, 16
	
	mov eax, edi	
	mov cr3, eax	; vuelvo al directorio del usuario
	;~
	 pop eax
	 mov es, ax
	 pop eax
	 mov ds, ax	
	
	mov eax, ebx	; guardo en eax la dir virtual que se devolverà
	pop esi	
	pop edi
	pop ebx
	sti
	iret





msg:
db "EAX            ",0
db "EBX            ",0
db "ECX            ",0
db "EDX            ",0
db "ESI            ",0
db "EDI            ",0
db "EBP            ",0
db "ESP            ",0

msg2:
db "CR0            ",0
db "CR2            ",0
db "CR3            ",0
db "CR4            ",0

msg3:
db "EFLAGS         ",0

msg4:
db "CS             ",0
db "DS             ",0
db "ES             ",0
db "FS             ",0
db "GS             ",0
db "SS             ",0

msg5:
db "Stack:      ",0
db "        ",0
db "        ",0
db "        ",0
db "        ",0
db "        ",0
db "        ",0

banda:
db "  Scorpions                                                                     ",0


_informarEstado:

	DWORD_TO_HEX eax, msg+7
	DWORD_TO_HEX ebx, msg+23
	DWORD_TO_HEX ecx, msg+39
	DWORD_TO_HEX edx, msg+55
	DWORD_TO_HEX edi, msg+71
	DWORD_TO_HEX esi, msg+87
	DWORD_TO_HEX ebp, msg+103
	mov eax, esp
	DWORD_TO_HEX eax, msg+119
	mov eax, cr0
	DWORD_TO_HEX eax, msg2+7
	mov eax, cr2
	DWORD_TO_HEX eax, msg2+23
	mov eax, cr3
	DWORD_TO_HEX eax, msg2+39
	mov eax, cr4
	DWORD_TO_HEX eax, msg2+55
	pushf
	pop eax
	DWORD_TO_HEX eax, msg3+7
	
	mov eax, cs
	DWORD_TO_HEX eax, msg4+7
	mov eax, ds
	DWORD_TO_HEX eax, msg4+23
	mov eax, es
	DWORD_TO_HEX eax, msg4+39
	mov eax, fs
	DWORD_TO_HEX eax, msg4+55
	mov eax, gs
	DWORD_TO_HEX eax, msg4+71
	mov eax, ss
	DWORD_TO_HEX eax, msg4+87	

	mov eax, [esp]
	DWORD_TO_HEX eax, msg5+13
	mov eax, [esp+4]
	DWORD_TO_HEX eax, msg5+22
	mov eax, [esp+8]
	DWORD_TO_HEX eax, msg5+31
	mov eax, [esp+12]
	DWORD_TO_HEX eax, msg5+40
	mov eax, [esp+16]
	DWORD_TO_HEX eax, msg5+49
	mov eax, [esp+20]
	DWORD_TO_HEX eax, msg5+58
	
	; REGISTROS DE PROPOSITO GENERAL
	push dword 1
	push dword 4
	push dword blanco_sobre_bordo
	push msg
	call kprintf
	add esp, 12
	push dword 5
	push dword blanco_sobre_bordo
	push msg+16
	call kprintf
	add esp, 12
	push dword 6
	push dword blanco_sobre_bordo
	push msg+32
	call kprintf
	add esp, 12
	push dword 7
	push dword blanco_sobre_bordo
	push msg+48
	call kprintf
	add esp, 12
	push dword 8
	push dword blanco_sobre_bordo
	push msg+64
	call kprintf	
	add esp, 12
	push dword 9
	push dword blanco_sobre_bordo
	push msg+80
	call kprintf		
	add esp, 12
	push dword 10
	push dword blanco_sobre_bordo
	push msg+96
	call kprintf			
	add esp, 12
	push dword 11
	push dword blanco_sobre_bordo
	push msg+112
	call kprintf			
	
	; REGISTROS DE CONTROL
	add esp, 12
	push dword 13
	push dword blanco_sobre_bordo
	push msg2
	call kprintf					
	add esp, 12
	push dword 14
	push dword blanco_sobre_bordo
	push msg2+16
	call kprintf					
	add esp, 12
	push dword 15
	push dword blanco_sobre_bordo
	push msg2+32
	call kprintf					
	add esp, 12
	push dword 16
	push dword blanco_sobre_bordo
	push msg2+48
	call kprintf								
	
	; EFLAGS
	add esp, 12
	push dword 18
	push dword blanco_sobre_bordo
	push msg3
	call kprintf
	
	; SELECTORES DE SEGMENTO
	add esp, 16
	push dword 21
	push dword 4
	push dword blanco_sobre_bordo
	push msg4
	call kprintf					
	add esp, 12
	push dword 5
	push dword blanco_sobre_bordo
	push msg4+16
	call kprintf					
	add esp, 12
	push dword 6
	push dword blanco_sobre_bordo
	push msg4+32
	call kprintf					
	add esp, 12
	push dword 7
	push dword blanco_sobre_bordo
	push msg4+48
	call kprintf								
	add esp, 12
	push dword 8
	push dword blanco_sobre_bordo
	push msg4+64
	call kprintf					
	add esp, 12
	push dword 9
	push dword blanco_sobre_bordo
	push msg4+80
	call kprintf				
	
	; STACK	
	add esp, 12
	push dword 11
	push dword blanco_sobre_bordo
	push msg5
	call kprintf				
	add esp, 12
	push dword 13
	push dword blanco_sobre_bordo
	push msg5+13
	call kprintf				
	add esp, 12
	push dword 14
	push dword blanco_sobre_bordo
	push msg5+22
	call kprintf				
	add esp, 12
	push dword 15
	push dword blanco_sobre_bordo
	push msg5+31
	call kprintf				
	add esp, 12
	push dword 16
	push dword blanco_sobre_bordo
	push msg5+40
	call kprintf				
	add esp, 12
	push dword 17
	push dword blanco_sobre_bordo
	push msg5+49
	call kprintf				
	add esp, 12
	push dword 18
	push dword blanco_sobre_bordo
	push msg5+58
	call kprintf				
	add esp, 16	
	
	; nombre del grupo
	push dword 0
	push dword 0
	push dword blanco_sobre_verde
	push banda
	call kprintf					
	add esp, 16		
	;~ 
	
noMostrar:	
	add esp, 4	;alinea el stack, sacando el codigo de error 
	
	jmp $

	

proximo_reloj:
	pushad
	inc DWORD [isrnumero]
	mov ebx, [isrnumero]
	cmp ebx, 0x4
	jl .ok
		mov DWORD [isrnumero], 0x0
		mov ebx, 0
	.ok:
		add ebx, isrmensaje1
		mov edx, isrmensaje
		IMPRIMIR_TEXTO edx, 6, 0x0A, 24, 73
		IMPRIMIR_TEXTO ebx, 1, 0x0A, 24, 79
	popad
	ret
	
isrmensaje: db 'Clock:'
isrnumero: dd 0x00000000
isrmensaje1: db '|'
isrmensaje2: db '/'
isrmensaje3: db '-'
isrmensaje4: db '\'

isr0_msg:  db"# 0 - Division by zero exception",0	;32
isr1_msg:  db"# 1 - Debug exception",0	;21
isr2_msg:  db"# 2 - Non maskable interrupt",0 ;28	
isr3_msg:  db"# 3 - Breakpoint exception",0 ;26	
isr4_msg:  db"# 4 - 'Into detected overflow'",0 ;30	
isr5_msg:  db"# 5 - Out of bounds exception",0  ;29
isr6_msg:  db"# 6 - Invalid opcode exception",0 ;30
isr7_msg:  db"# 7 - No coprocessor exception",0	;30
isr8_msg:  db"# 8 - Double fault (pushes an error code)",0	;41
isr9_msg:  db"# 9 - Coprocessor segment overrun",0;33	
isr10_msg:  db"# 10 - Bad TSS (pushes an error code)",0	;38
isr11_msg:  db"# 11 - Segment not present (pushes an error code)",0	;50
isr12_msg:  db"# 12 - Stack fault (pushes an error code)",0;42	
isr13_msg:  db"# 13 - General protection fault (pushes an error code)",0;55	
isr14_msg:  db"# 14 - Page fault (pushes an error code)",0 ;41	
isr15_msg:  db"# 15 - Unknown interrupt exception",0;35	
isr16_msg:  db"# 16 - Coprocessor fault",0;25	
isr17_msg:  db"# 17 - Alignment check exception",0	;33
isr18_msg:  db"# 18 - Machine check exception",0;31	
isr19_msg:  db"# 19 - SIMD Floating Point Exception",0	

blanco_sobre_bordo equ 01001111b
blanco_sobre_verde equ 00101111b
